package cn.edu.sdjzu.xg.kcsj.security;

import cn.edu.sdjzu.xg.kcsj.exception.BysjException;
import cn.edu.sdjzu.xg.kcsj.util.JdbcHelper;
import cn.edu.sdjzu.xg.kcsj.util.Pagination;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.Collection;
import java.util.List;
import java.util.TreeSet;

public class ResourceAssRoleService {
    private static ResourceAssRoleDao resourceAssRoleDao = ResourceAssRoleDao.getInstance();
    private static ResourceAssRoleService resourceAssRoleService = new ResourceAssRoleService();

    public static ResourceAssRoleService getInstance(){
        return resourceAssRoleService;
    }


    public Collection<ResourceAssRole> findAll(List conditionList, Pagination pagination) throws SQLException {
        Connection connection = JdbcHelper.getConn();
        //将自动提交设为false,开始事务
        connection.setAutoCommit(false);
        Collection<ResourceAssRole> resourceAssRoles = new TreeSet<ResourceAssRole>();
        try {
            resourceAssRoles = resourceAssRoleDao.findAll(conditionList,pagination,connection);
            //提交事务
            connection.commit();
        }catch (SQLException e){
            //回滚
            connection.rollback();
        }finally {
            //将自动提交设为true，结束事务
            connection.setAutoCommit(true);
            JdbcHelper.close(connection);
        }
        return resourceAssRoles;
    }


    public ResourceAssRole find(Integer id) throws SQLException {
        Connection connection = JdbcHelper.getConn();
        //将自动提交设为false,开始事务
        connection.setAutoCommit(false);
        ResourceAssRole resourceAssRole = null;
        try {
            resourceAssRole = resourceAssRoleDao.find(id,connection);
            //提交事务
            connection.commit();
        }catch (SQLException e){
            //回滚
            connection.rollback();
        }finally {
            //将自动提交设为true，结束事务
            connection.setAutoCommit(true);
            JdbcHelper.close(connection);
        }
        return resourceAssRole;
    }

    // 更新一条
    public boolean update(ResourceAssRole resourceAssRole) throws SQLException {
        Connection connection = JdbcHelper.getConn();
        //将自动提交设为false,开始事务
        connection.setAutoCommit(false);
        Boolean updated =false;
        try {
            updated = resourceAssRoleDao.update(resourceAssRole,connection);
            //提交事务
            connection.commit();
        }catch (SQLException e){
            //回滚
            connection.rollback();
        }finally {
            //将自动提交设为true，结束事务
            connection.setAutoCommit(true);
            JdbcHelper.close(connection);
        }
        return updated;
    }

    public boolean add(ResourceAssRole resourceAssRole) throws SQLException {
        Connection connection = JdbcHelper.getConn();
        //将自动提交设为false,开始事务
        connection.setAutoCommit(false);
        Boolean added =false;
        try {
            //增加
            added = resourceAssRoleDao.add(resourceAssRole,connection);
            //提交事务
            connection.commit();
        }catch (SQLException e){
            //回滚
            connection.rollback();

        }finally {
            //将自动提交设为true，结束事务
            connection.setAutoCommit(true);
            JdbcHelper.close(connection);
        }
        return added ;
    }

    // 删除
    public boolean delete(Integer id) throws SQLException, BysjException {
        Connection connection = JdbcHelper.getConn();
        //将自动提交设为false,开始事务
        connection.setAutoCommit(false);
        Boolean deleted = false;
        try {
            deleted = resourceAssRoleDao.delete(id, connection);
            //提交事务
            connection.commit();
        } catch (SQLException e) {
            //回滚
            connection.rollback();
        } finally {
            //将自动提交设为true，结束事务
            connection.setAutoCommit(true);
            JdbcHelper.close(connection);
        }
        return deleted;
    }

    public Collection<Resource> findResourcesByRoleId(int id) throws SQLException {
        Collection<Resource> resources = new TreeSet<>();
        Connection connection = JdbcHelper.getConn();
        //将自动提交设为false,开始事务
        connection.setAutoCommit(false);
        try {
            resources = resourceAssRoleDao.findResourcesByRoleId(id, connection);
            //提交事务
            connection.commit();
        } catch (SQLException e) {
            //回滚
            connection.rollback();
        } finally {
            //将自动提交设为true，结束事务
            connection.setAutoCommit(true);
            JdbcHelper.close(connection);
        }
        return resources;
    }

    public void batchInsert(List<ResourceAssRole> resourceAssRoles) throws SQLException {
        Connection connection = JdbcHelper.getConn();
        try {
            connection.setAutoCommit(false);
            resourceAssRoleDao.truncate(connection);
            for (ResourceAssRole resourceAssRole : resourceAssRoles) {
                resourceAssRoleDao.add(resourceAssRole, connection);
            }
            connection.commit();
        } catch (Exception e) {
            connection.rollback();
        } finally {
            connection.setAutoCommit(true);
            JdbcHelper.close(connection);
        }
    }
}
